package com.tansun.easycare.rule.approval.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.tansun.easycare.rule.approval.entity.RuleManageInfo;
import com.tansun.easycare.rule.approval.entity.RulePublishApproval;
import com.tansun.easycare.rule.approval.service.RulePublishApprovalService;
import com.tansun.easycare.rule.datamodel.service.RuleDataparamsService;
import com.tansun.easycare.rule.deployonline.service.RuleDeployOnlineService;
import com.tansun.easycare.rule.rulemange.service.RuleManageService;
import com.tansun.rule.utils.StringUtils;
import com.tansun.workflow.api.IWorkflowController;
import com.tansun.workflow.model.TaskInfo;

@Service("publishApproval")
public class RulePublishApprovalControService implements IWorkflowController {


    @Autowired
    private RuleManageService ruleManageService;

    @Autowired
    private RulePublishApprovalService rulePublishApprovalService;
    @Autowired
    private RuleDataparamsService ruleDataparamsService;
    @Autowired
    private RuleDeployOnlineService ruleDeployOnlineService;

    @Override
    public void processCompletedTrans(Map<String, Object> map, String s) {
        String businessKey = MapUtils.getString(map, "businessKey");
        Map<?,?> processVariables = MapUtils.getMap(map, "processVariables");
        String isPass = MapUtils.getString(processVariables, "isPass");
        if ("N".equals(isPass)){
            return;
        }
        List<String> ruleManageIds = new ArrayList<>();
        List<String> funcParamsIds = new ArrayList<>();
        List<String> deploymentIds = new ArrayList<>();
        RulePublishApproval rulePublishApproval = rulePublishApprovalService.get(businessKey);
        String ruleUpdateDetail = rulePublishApproval.getRuleUpdateDetail();
        System.out.println("审批结果：" + ("Y".equals(isPass)?"同意": "不同意" )+ ", ruleUpdateDetail ===>" +ruleUpdateDetail );
        if(StringUtils.isNotBlank(ruleUpdateDetail)){
            List<RuleManageInfo> infos = JSON.parseArray(ruleUpdateDetail, RuleManageInfo.class);
            for (RuleManageInfo info : infos) {
                //区分函数变量还是rulemanage下的
                if(info.getCode().equals("drl")||info.getCode().equals("format")){
                    funcParamsIds.add(info.getId());
                }else if(info.getCode().equals("deployment")){
                    deploymentIds.add(info.getId());
                } else{
                    ruleManageIds.add(info.getId());
                }
            }
            if(ruleManageIds!=null&&ruleManageIds.size()>0){
                ruleManageService.updateActiveVersion(ruleManageIds);
            }
            if(funcParamsIds!=null&&funcParamsIds.size()>0){
                ruleDataparamsService.complementApproval(funcParamsIds);
            }
            if(deploymentIds!=null&&deploymentIds.size()>0){
                ruleDeployOnlineService.updateStatusIndById(deploymentIds);
            }
        }
    }

    @Override
    public void afterVotingCompleted(Map<String, Object> map) {

    }


    @Override
    public Map<String, Object> beforeStartProcessTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> afterStartProcessTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> beforeSubmitTaskTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> afterSubmitTaskTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> beforeBackPreTaskTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> afterBackPreTaskTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> beforeRebackTaskTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> afterRebackTaskTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> beforeSuspendProcessTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> afterSuspendProcessTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> beforeActivateProcessTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> afterActivateProcessTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> beforeTransferTaskTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> afterTransferTaskTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> beforeBackFirstTaskTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> afterBackFirstTaskTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> beforeBackTaskTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> afterBackTaskTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> beforeTerminateProcessTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public Map<String, Object> afterTerminateProcessTrans(Map<String, Object> map) {
        return null;
    }

    @Override
    public void createTaskEvent(TaskInfo taskInfo) {

    }
}
